<?php


namespace app\common\model;


class AuthGroup extends ModelBase
{
    /**
     * select auth list
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAuthGroupList(array $where = [], string $field = '*', int $page = 0, int $limit = 10): array
    {
        $query = $this->where($where)->field($field);
        !empty($page) ? $list = $query->paginate(['list_rows' => $limit, 'page' => $page]) : $list = $query->select();
        return $list ? $list->toArray() : [];
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAuthGroupInfo(array $where = [], string $field = '*'): array
    {
        $info = $this->where($where)->field($field)->find();
        return $info ? $info->toArray() : [];
    }

    public function getNoPageMenuList($where = [], $field = true, $order = 'id asc'){
        $list =   $this->where($where)->field($field)->order($order)->select();
        $list = !empty($list) ? $list->toArray() : [];
        return $list;
    }

    /**
     * get auth group list
     * @param $where
     * @param string $field
     * @param string $order
     * @return array|array[]|\array[][]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAdminAuthGroupList($where, $field = '*', $order = '')
    {
        $this->join = [['auth_group_access aga', 'ag.id = aga.group_id', 'left']];
        $query = $this->where($where)->field($field)->where('ag.status', '<>', -1)->order($order);

        $list = $query->alias('ag')->leftJoin(SYS_DB_PREFIX . 'auth_group_access aga', 'ag.id = aga.group_id')->select();

        $this->join = [];
        return $list ? $list->toArray() : [];
    }
}
